package com.itheima.movement.controller;

import com.itheima.entity.dto.MovementDto;
import com.itheima.entity.pojo.Movement;
import com.itheima.entity.vo.MovementVo;
import com.itheima.entity.vo.PagingQueryVo;
import com.itheima.entity.vo.Result;
import com.itheima.movement.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author :WuJunJie
 * packageName :com.itheima.movement.controller
 * fileName :MovementController
 * creationTime :2022-12-10 19:47
 */
@RestController
@RequestMapping("/movementService/movement")
public class MovementController {
    @Autowired
    private MovementService movementService;

    @PostMapping("/insert")
    public Result insertMovement(MovementDto movementDto) throws Exception {
        Boolean insert = movementService.insertMovement(movementDto);
        if(insert){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 软删
     * @param id 动态ID
     * @return
     */
    @PostMapping("/delete")
    public Result deleteMovement(@RequestParam String id){
        Boolean delete=movementService.deleteMovement(id);
        if (delete){
            return Result.success();
        }
        return Result.error();
    }

    @PutMapping()
    public Result updateMovement(@RequestBody MovementDto movementDto){
        Boolean updateMovement=movementService.updateMovement(movementDto);
        if (updateMovement){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 查询
     * @param type 查询的类型，0为查询所有动态,1位查询自己的动态
     * @param page   页码
     * @param pageSize  页码大小
     * @return
     */
    @GetMapping()
    public Result selectAllMovement(@RequestParam(name = "type") Integer type,
                                    @RequestParam(name = "page",defaultValue = "1")Integer page,
                                    @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize){
        PagingQueryVo<MovementVo> movementVos=movementService.selectAllMovement(type,page,pageSize);
        return Result.success(movementVos);
    }



}
